package com.data;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.cards.Football;


public class footballTable extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int    DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME    = "db_football";

    // Contacts table name
    private static final String TABLE_FOOTBALL   = "tbl_footbal";

    // Contacts Table Columns names
    private static final String ID               = "id";
    private static final String HOZUR            = "hozur";
    private static final String GC               = "gc";
    private static final String GI               = "gi";
    private static final String GO               = "go";
    private static final String SCORE            = "score";


    /**
     * Constructor
     * 
     * @param context to use to open or create the database
     * */
    public footballTable(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    // Creating Tables

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_FOOTBALL_TABLE = TABLE_FOOTBALL + "("
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + HOZUR + " INTEGER,"
                + GC + " INTEGER," + GI + " INTEGER, " + GO + " INTEGER," + SCORE + " INTEGER" + ");";
        db.execSQL("CREATE TABLE IF NOT EXISTS " + CREATE_FOOTBALL_TABLE);
    }


    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOTBALL);

        // Create tables again
        onCreate(db);
    }


    public long addFootball(Football football) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(HOZUR, football.getHozur());
        values.put(GC, football.getGameCount());
        values.put(GI, football.getGoalIn());
        values.put(GO, football.getGoalOut());
        values.put(SCORE, football.getScore());

        // Inserting Row
        long result = db.insert(TABLE_FOOTBALL, null, values);
        db.close(); // Closing database connection
        return result;
    }


    Football getFootball(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_FOOTBALL, new String[]{ ID, HOZUR, GC, GI,
                GO, SCORE }, ID + "=?",
                new String[]{ String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Football football = new Football(cursor.getInt(0), cursor.getInt(1),
                cursor.getInt(2), cursor.getInt(3),
                cursor.getInt(4), cursor.getInt(5));

        db.close();
        cursor.close();

        // return football
        return football;
    }


    public List<Football> getAllMotors() {
        List<Football> footballList = new ArrayList<Football>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_FOOTBALL;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                Football football = new Football(cursor.getInt(0), cursor.getInt(1),
                        cursor.getInt(2), cursor.getInt(3),
                        cursor.getInt(4), cursor.getInt(5));

                // Adding football to list
                footballList.add(football);
            }
            while (cursor.moveToNext());
        }

        db.close();
        cursor.close();

        // return footballList
        return footballList;
    }


    public int updateFootball(Football football) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(HOZUR, football.getHozur());
        values.put(GC, football.getGameCount());
        values.put(GI, football.getGoalIn());
        values.put(GO, football.getGoalOut());
        values.put(SCORE, football.getScore());

        // updating row
        int result = db.update(TABLE_FOOTBALL, values, ID + " = ?",
                new String[]{ String.valueOf(football.getId()) });
        db.close();
        return result;
    }


    public int deleteContact(Football football) {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete(TABLE_FOOTBALL, ID + " = ?",
                new String[]{ String.valueOf(football.getId()) });
        db.close();
        return result;
    }


    public int getNumberOfFootball() {
        String countQuery = "SELECT  * FROM " + TABLE_FOOTBALL;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int result = cursor.getCount();

        db.close();
        cursor.close();

        return result;
    }

}